<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Ver 3.0, Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/
class ClsUser extends ClsNaanalController
{
    public function __construct()
    {
        parent::__construct();
    }
    public function create()
    {
        $form=ClsWrapper::getInstance("user");
        return array('form'=>$form,'issubmit'=>false);
    }

    function edit()
    {
        $obj=new ClsWUser();
        $obj->loadObject(ClsNaanalRequest::getInstance()->getData("id"));
        return array('form'=>$obj);
    }

    function show_list()
    {
        $this->action='list';
        $arrWhere=array();
        $arrWhere[]=array("field"=>"client_id","data"=>  ClsNaanalSession::getInstance()->getPanelData("client_id"));
        $objWUser=  ClsWrapper::getInstance("user");
        $arrList["list"] = $objWUser->getAssocList($arrWhere);
        return $arrList;
    }
    public function activate()
    {
        $this->objPDO->setQuery("update user set active=1 where id = ".ClsNaanalRequest::getInstance()->getData("id"));
        $this->redirect="{$this->urlModuleParam}=user&action=list";
    }
    public function deactivate()
    {
        $this->objPDO->setQuery("update user set active=0 where id = ".ClsNaanalRequest::getInstance()->getData("id"));
        $this->redirect="{$this->urlModuleParam}=user&action=list";
    }
    function update()
    {
        $obj=new ClsWUser();
        $arrRequest=  ClsNaanalRequest::getInstance()->getAll();
        $obj->loadObject($arrRequest["id"]);
        $obj->setArray($arrRequest);
        $obj->update();
        $pagevar=ClsConfig::$PAGINATION_VAR;
        $curpage=isset($_REQUEST[$pagevar])?$_REQUEST[$pagevar]:1;
        $arrPermission=getUserPermission();
        if(isAdminUser() || isset($arrPermission["user"]["list"]))
        {
            $this->redirect="{$this->urlModuleParam}=user&{$this->urlActionParam}=list&{$pagevar}={$curpage}";
        }
        else
        {
            $this->redirect="{$this->urlModuleParam}=user&{$this->urlActionParam}=edit&id={$arrRequest["id"]}";
        }
    }
    public function delete()
    {
        $this->objPDO->setQuery("delete from user where id = ".ClsNaanalRequest::getInstance()->getData("id"));
        $pagevar=ClsConfig::$PAGINATION_VAR;
        $curpage=isset($arrRequest[$pagevar])?$arrRequest[$pagevar]:1;
        $this->redirect="{$this->urlModuleParam}=user&{$this->urlActionParam}=list&{$pagevar}={$curpage}";
    }
    function insert()
    {
        $obj=new ClsWUser();
        $arrRequest=  ClsNaanalRequest::getInstance()->getAll();
        $obj->set_client_id(getUserObject()->get_client_id());
        $obj->setArray($arrRequest);
        $obj->update();
        $pagevar=ClsConfig::$PAGINATION_VAR;
        $curpage=isset($_REQUEST[$pagevar])?$_REQUEST[$pagevar]:1;
        $this->redirect="{$this->urlModuleParam}=user&{$this->urlActionParam}=list&{$pagevar}={$curpage}";
    }
     /**
    * reset password
    * @return message to template
    */
   function forgot()
   {
       $request=  ClsNaanalRequest::getInstance()->getAll();
           $sql="select * from `user` where email='".$request["email"]."'";
           $this->objPDO->setQuery($sql);
           $obj=$this->objPDO->getObject("ClsWUser");
           if($obj)
           {
                   $to=$request["email"];
                   $subject="Forgot Password";
                   $pass=(string)rand(101101, 999999);
                   $randword="sd#&^uiv12389";;
                   $newpass="";
                   for($i=0;$i<strlen($pass);$i++)
                   {
                           $newpass.=$randword[$pass[$i]];	
                   }
                   $obj->set_password($newpass);
                   $sql="update `user` set password='{$newpass}' where `id`=:id";
                   $this->objPDO->setQuery($sql,$obj);
                   $message = "The password password is ".$newpass;
                   $from = "admin@localhost";
                   $sql="select * from organization where name='email'";
                   $this->objPDO->setQuery($sql);
                   $arrAssoc=$this->objPDO->getAllAssoc();
                   if($arrAssoc)
                   {
                        $from=$arrAssoc[0]["value"];
                   }
                   $headers = "From: $from";
                   $send=mail($to,$subject,$message,$headers);
                   if($send) $msg="Mail Send Successfully";
                   else $msg="Mailbox unavailable";
           }
           else 
           {
                   $msg="UnKnown EMail Address";
           }
           echo $msg;
           exit;
   }
} 

?>
